/*******************************************************************************
 *    系统名称   : 约汗乐动力接口/后台管理系统
 *    客户             : 约汗乐动力
 *    文件名        ： SysContent.java
 *              (C) Copyright 博智乐创 Corporation 2015
 *               All Rights Reserved.
 *              http://www.bozlc.com
 * *****************************************************************************
 *    注意： 本内容仅限于约汗乐动力软件公司内部使用，未经许可禁止转发
 ******************************************************************************/
package com.xiake.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


// TODO: Auto-generated Javadoc
/**
 * The Class SysContent.
 */
public class SysContent {  
     
     /** The request local. */
     private static ThreadLocal<HttpServletRequest> requestLocal= new ThreadLocal<HttpServletRequest>();  
     
     /** The response local. */
     private static ThreadLocal<HttpServletResponse> responseLocal= new ThreadLocal<HttpServletResponse>();  
       
    /**
     * Gets the request.
     *
     * @return the request
     */
    public static HttpServletRequest getRequest() {  
        return (HttpServletRequest)requestLocal.get();  
    }  
    
    /**
     * Sets the request.
     *
     * @param request the new request
     */
    public static void setRequest(HttpServletRequest request) {  
        requestLocal.set(request);  
    }  
    
    /**
     * Gets the response.
     *
     * @return the response
     */
    public static HttpServletResponse getResponse() {  
        return (HttpServletResponse)responseLocal.get();  
    }  
    
    /**
     * Sets the response.
     *
     * @param response the new response
     */
    public static void setResponse(HttpServletResponse response) {  
        responseLocal.set(response);  
    }  
    
    /**
     * Gets the session.
     *
     * @return the session
     */
    public static HttpSession getSession() {  
        return (HttpSession)((HttpServletRequest)requestLocal.get()).getSession();  
    }  
    
    public static String getIp(){
    	
    	String ip = SysContent.getRequest().getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = SysContent.getRequest().getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = SysContent.getRequest().getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = SysContent.getRequest().getRemoteAddr();
        }
        return ip;
    	
    }
    
    public static String getIp(HttpServletRequest request){
    	String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}